package etcd

import (
	"encoding/json"
	"os"
	"time"
)

type ServiceRegisterInfo struct {
	SrName           string `json:"sr_name"`
	SrInvoke         string `json:"sr_invoke"`
	CreateTime       int64  `json:"create_time"`
	CreateTimeFormat string `json:"create_time_format"`
	LeaseTime        int64  `json:"lease_time"`
	HostName         string `json:"hostname"`
}

func NewRegisterInfo() *ServiceRegisterInfo {
	now := time.Now()
	hostname, _ := os.Hostname()

	return &ServiceRegisterInfo{
		CreateTime:       now.UnixNano(),
		CreateTimeFormat: now.Format(TIME_LAYOUT),
		HostName:         hostname,
	}
}

func (r *ServiceRegisterInfo) String() string {
	s, _ := json.Marshal(r)
	return string(s)
}

func (r *ServiceRegisterInfo) Parse(s string) error {
	return json.Unmarshal([]byte(s), r)
}
